﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RM2012Mobile
{
    public partial class frmTablesEmpty : Form
    {
        RM2012Service.RM2012Service objService = new RM2012Service.RM2012Service();
        DataSet ds;
        public static string tablesIdPublic = "NOTHING";//--
        public static string tablesNamePublic = "NOTHING";//--
        public static string invoiceIdPublic = "NOTHING";
        public static string menuIdPublic = "NOTHING";
        public static string menuNamePublic = "NOTHING";
        public static string objectNamePublic = "INVOICE";
        public static string actionPublic = "ADD";
        public static string numOldPublic = "NOTHING";
        public static string numNewPublic = "NOTHING";
        public static string usernamePublic = Utilities.UsernamePublic;
        public static string fullnamePublic = Utilities.FullNamePublic;
        public static string groupnamePublic = Utilities.GroupNamePublic;

        public frmTablesEmpty()
        {
            InitializeComponent();
        }

        private void frmTablesEmpty_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        public string AddCustomerNew()
        {
            RM2012Service.CustomerInfo info = new RM2012Service.CustomerInfo();
            DataSet dsLast = objService.GetCustomerLastDataSet();
            DataTable dtLast = dsLast.Tables[0];
            string cusIdStr = dtLast.Rows[0]["CusId"].ToString();
            int cusIdInt = Convert.ToInt32(cusIdStr.Substring(3)) + 1;
            string cusId = "";
            if (cusIdInt < 10)
            {
                cusId = "CUS" + "00" + cusIdInt.ToString();
            }
            else if ((cusIdInt >= 10) && (cusIdInt < 100))
            {
                cusId = "CUS" + "0" + cusIdInt.ToString();
            }
            else
            {
                cusId = "CUS" + cusIdInt.ToString();
            }
            info.CusId = cusId;
            info.FullName = "Khách lẻ";
            info.Address = "";
            info.Sex = true;
            info.DiscountPer = 0;
            info.Debt = 0;
            info.Phone = "";
            info.Email = "";
            info.State = "";
            info.Remark = "";
            bool checkAdd = objService.AddCustomer(info);
            if (checkAdd == true)
            {
                bool checkUpdate = objService.UpdateCustomerSex(info.CusId);
                if (checkUpdate == true)
                {
                    cusId = info.CusId;
                }
            }
            return cusId;
        }

        public void NoticeLog()
        {
            try
            {
                string time = Utilities.GetNow();
                string message = Utilities.CreateMessage(tablesIdPublic, tablesNamePublic, invoiceIdPublic, menuIdPublic, menuNamePublic, objectNamePublic, actionPublic, numOldPublic, numNewPublic, usernamePublic, fullnamePublic, groupnamePublic, time);

                MyUdpClient client = new MyUdpClient();
                client.OnConnect(message);
            }
            catch
            {
                MessageBox.Show("Chưa gửi được !");
            }
        }

        public void RefreshGrid()
        {
            ds = new DataSet();
            ds = objService.GetTableByStateDataSet(1);
            gridTablesEmpty.DataSource = ds.Tables[0];
            if (ds.Tables[0].Rows.Count > 0)
            {
                tablesIdPublic = gridTablesEmpty[0, 0].ToString();
                tablesNamePublic = gridTablesEmpty[0, 1].ToString();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int rowIndex = gridTablesEmpty.CurrentRowIndex;
            string tablesId = gridTablesEmpty[rowIndex, 0].ToString();
            bool resultUpdate = objService.UpdateTableState(tablesId, 2);
            if (resultUpdate == true)
            {
                RefreshGrid();
                MessageBox.Show("Đã cập nhật !");
            }
            else
            {
                MessageBox.Show("Làm lại !");
            }
        }

        private void btnAddInvoice_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = gridTablesEmpty.CurrentRowIndex;
                string tablesId = gridTablesEmpty[rowIndex, 0].ToString();
                RM2012Service.InvoiceInfo info = new RM2012Service.InvoiceInfo();
                //Id
                DateTime dateNow = DateTime.Now;
                string _year = dateNow.Year.ToString();
                string _month = dateNow.Month.ToString();
                if (dateNow.Month < 10)
                {
                    _month = "0" + dateNow.Month.ToString();
                }
                string _day = dateNow.Day.ToString();
                if (dateNow.Day < 10)
                {
                    _day = "0" + dateNow.Day.ToString();
                }
                string _hour = dateNow.Hour.ToString();
                if (dateNow.Hour < 10)
                {
                    _hour = "0" + dateNow.Hour.ToString();
                }
                string _minute = dateNow.Minute.ToString();
                if (dateNow.Minute < 10)
                {
                    _minute = "0" + dateNow.Minute.ToString();
                }
                string _second = dateNow.Second.ToString();
                if (dateNow.Second < 10)
                {
                    _second = "0" + dateNow.Second.ToString();
                }
                string invoiceId = _year + _month + _day + _hour + _minute + _second;
                info.InvoiceId = invoiceId;
                info.InvoiceDate = DateTime.Now;
                info.CusId = AddCustomerNew();
                info.TablesId = tablesId;
                info.Total = 0.0;
                info.Paid = 0.0;
                info.IsDeleted = false;
                info.IsPaid = false;
                info.DiscountPer = 0.0;
                info.Remark = "";
                if (!info.CusId.Equals(""))
                {
                    if (objService.AddInvoice(info) == true)
                    {
                        ServerLoggingNotice();
                        MessageBox.Show("Đã tạo phục vụ !");
                    }
                    else
                    {
                        MessageBox.Show("Thực hiện lại !");
                    }
                }
            }
            catch
            {
                MessageBox.Show("Thực hiện lại !");
            }
        }

        private void btnNotice_Click(object sender, EventArgs e)
        {
            ServerLoggingNotice();
        }

        public void ServerLoggingNotice()
        {
            Thread tClient = new Thread(new ThreadStart(NoticeLog));
            tClient.Start();
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void menuItemUpdateState_Click(object sender, EventArgs e)
        {
            btnUpdate_Click(sender, e);
        }

        private void menuItemAddInvoice_Click(object sender, EventArgs e)
        {
            btnAddInvoice_Click(sender, e);
        }

        private void menuItemNotice_Click(object sender, EventArgs e)
        {
            btnNotice_Click(sender, e);
        }

        private void gridTablesEmpty_Click(object sender, EventArgs e)
        {
            int rowIndex = gridTablesEmpty.CurrentRowIndex;
            tablesIdPublic = gridTablesEmpty[rowIndex, 0].ToString();
            tablesNamePublic = gridTablesEmpty[rowIndex, 1].ToString();
        }
    }
}